﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Web;
using System.Runtime.Remoting.Messaging;

namespace PushTask
{
    #region 日志助手類
    /// <summary>
    /// 日志助手類
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// 寫入日志
        /// </summary>
        /// <param name="logType">日志類型</param>
        /// <param name="content">日志內容</param>
        public static void WriteLongLog(string logType, string content)
        {
            string newLine = System.Environment.NewLine;
            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var index = baseDirectory.IndexOf("bin", StringComparison.OrdinalIgnoreCase);
            if (index > 0)
            {
                baseDirectory = baseDirectory.Remove(index);
            }

            string path = baseDirectory + @"log\" + logType + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + " - " + logType.ToString() + ".txt";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            }
            try
            {
                //采用追加模式来写日志
                using (var fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Write))
                {
                    StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
                    var str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + newLine + content + newLine + newLine;
                    writer.WriteLine(str);
                    writer.Flush();
                }
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 始終都是創建新的文件
        /// </summary>
        /// <param name="content"></param>
        public static void WriteCreateNewLog(string logType, string content)
        {
            string newLine = System.Environment.NewLine;
            var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            var index = baseDirectory.IndexOf("bin", StringComparison.OrdinalIgnoreCase);
            if (index > 0)
            {
                baseDirectory = baseDirectory.Remove(index);
            }

            string path = baseDirectory + @"log\" + logType + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + " - " + logType.ToString() + ".txt";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(path));
            }
            try
            {
                using (var fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    StreamWriter writer = new StreamWriter(fs, Encoding.UTF8);
                    var str = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + newLine + content + newLine + newLine;
                    writer.WriteLine(str);
                    writer.Flush();
                }
            }
            catch (Exception)
            {

            }
        }        
    }
    #endregion
}
